/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * sqlConnection.java
 *
 * Created on Mar 12, 2011, 8:41:45 AM
 */

package Apogeesrc;
import java.sql.*;
import javax.swing.*;
/**
 *
 * @author Chrysler
 */
public class sqlConnection extends javax.swing.JFrame {
private  String URL= "jdbc:odbc:apogee";
private  String user = "root";
private  String pass = "chrisujt782";
private ResultSet rs;
private Statement st;
    /** Creates new form sqlConnection */
    public sqlConnection() {
       initComponents();
    }

     public Connection openConnection() {
          Connection conn = null;
          try {
              Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
              conn = DriverManager.getConnection(URL, user, pass);
              //JOptionPane.showMessageDialog(null, "Connection Opened");
          }
          catch(Exception e) {
            new JOptionPane().showMessageDialog(null, e.getMessage());
          }
          return conn;
     }

     public void closeConnection(Connection conn){
        try{
            conn.close();
        }catch(Exception e){new JOptionPane().showMessageDialog(null, e.getMessage());}
     }

    public ResultSet select(Connection conn, String query){
        int ctr;
        try{
            st = conn.createStatement();
            rs = st.executeQuery(query);
        }catch(Exception e){new JOptionPane().showMessageDialog(null, e.getMessage());}

        if(rs == null)
            new JOptionPane().showMessageDialog(null, "Select query returned null");
        return rs;
    }


     public void update(Connection conn){}

     public String[] copyToArr(Connection conn, String[] arr, ResultSet rs, String colName){
         int ctr;
         try{
            for(ctr = 0;rs.next();ctr++){
                arr[ctr] = rs.getString(colName);
            }
        }catch(Exception e){new JOptionPane().showMessageDialog(null, e.getMessage()+" from copyToArr()");}

        return arr;
     }
     

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new sqlConnection().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
